#include "SocketMatTransmissionClient.h"
 
int main()
{

	Mat srcImage ;
	waitKey(0);
	SocketMatTransmissionClient socketMat;
	if (socketMat.socketConnect("127.0.0.1", 6666) < 0)
	{
		return 0;
	}
	int num = 0;
	VideoCapture cap; 
	cap.open("boat.avi");
	Mat frame;
	string res;

	struct infohead head;
	head.detectType =1;
	head.width =IMG_WIDTH;
	head.height =IMG_HEIGHT;
	head.packageNum =PACKAGE_NUM;
	socketMat.transmit(head);

	while(1){
		
		//char file[10] = {0};
		//sprintf(file, "bus/%d.jpg", (num++)%140);
		//srcImage = imread(file);
		
		cap>>srcImage; 
		if (srcImage.empty())
			break;
		cv::resize(srcImage, srcImage, cv::Size(IMG_WIDTH, IMG_HEIGHT));		
		socketMat.transmit(srcImage);
		string res = socketMat.receive();
		cout<<"in client res: "<< res <<endl;

		
		Json::Reader reader;
		Json::Value ret, rectangular;
		
		char res_str[40];
		if(reader.parse(res,ret))
		{
			if(!ret["label"].isNull())
			{
				cout<<ret["label"].asString()<<endl;
				cout<<ret["probability"].asDouble()<<endl;
				
				rectangular = ret["rect"];
				sprintf(res_str, "%s, %.2f",ret["label"].asString().c_str(),ret["probability"].asDouble());

				putText(srcImage,res_str,Point(25,30),FONT_HERSHEY_SIMPLEX,1,Scalar(255,23,0),4,8);
				Rect rect(rectangular["x"].asInt(), rectangular["y"].asInt(), rectangular["w"].asInt(), rectangular["h"].asInt());
				cv::rectangle(srcImage, rect, CV_RGB(0,255,255),2,CV_AA,0);
				cv::imshow("read",srcImage);
				cv::waitKey(3);
			}
		}

	}
	

 
	socketMat.socketDisconnect();
	return 0;
}